package com.rd.p2p.app.borrow;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.BorrowCollectionService;
import com.qdlc.p2p.biz.service.BorrowTenderService;
import com.qdlc.p2p.biz.service.BorrowUploadService;
import com.qdlc.p2p.biz.service.UserAutoTenderLogService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.BorrowUpload;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.BorrowCollectionModel;
import com.qdlc.p2p.dal.model.BorrowTenderModel;

/**
 * 我的投资
 * 
 * @author cx
 * @version 2.0
 * @since 2014年3月17日
 */
public class AppMyInvestAction extends BaseAction<BorrowTenderModel> implements ModelDriven<BorrowTenderModel> {

	@Resource
	private BorrowCollectionService borrowCollectionService;

	@Resource
	private BorrowTenderService borrowTenderService;
	@Resource
	private BorrowUploadService borrowUploadService;

	@Resource
	private UserAutoTenderLogService userAutoTenderLogService;

	private User user;

	private Map<String, Object> data;

	/**
	 * 我的投资列表 ajax数据接口 status
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/app/user/investList")
	public void mineList() throws Exception {
		data = new HashMap<String, Object>();
		user = getSessionUser();
		if (user == null) {
			data.put("resCode", "0");
			data.put("resMsg", "用户尚未登录!");
			printWebJson(getStringOfJpaObj(data));
			return;
		}
		model.setUser(user);
		PageDataList<BorrowTenderModel> pageDataList = borrowTenderService.list(model);

		if (pageDataList.getList().size() > 0) {
			for (int i = 0; i < pageDataList.getList().size(); i++) {
				BorrowTenderModel borrowModel = pageDataList.getList().get(i);
				List<BorrowUpload> pics = borrowUploadService.findPicByBorrowIdAndType(borrowModel.getBorrowId());
				if (pics.size() > 0) {
					borrowModel.setBorrowPicurl(pics.get(0).getPicPath());
				}
			}
		}

		data.put("data", pageDataList);
		data.put("resCode", "1");
		printWebJson(getStringOfJpaObj(data));
	}

	/**
	 * 我的待收列表 ajax数据接口 status=0待收 1已收 start_time end_time
	 * 
	 * @return
	 */
	@Action("/app/user/collectionList")
	public void collectionList() throws Exception {
		user = getSessionUser();
		BorrowCollectionModel m = (BorrowCollectionModel) paramModel(BorrowCollectionModel.class);
		int status = paramInt("status");
		m.setStatus(status);
		m.setUser(user);
		m.setPage(model.getPage());
		PageDataList<BorrowCollectionModel> pageDataList = borrowCollectionService.list(m);
		data = new HashMap<String, Object>();
		data.put("data", pageDataList);
		data.put("resCode", "1");
		printWebJson(getStringOfJpaObj(data));
	}

	/**
	 * 用户投资的借款标列表
	 * 
	 * @return
	 */
	@Action("/app/user/myInvestList")
	public void myInvestList() throws Exception {
		model.setUserId(getSessionUserId());
		model.setSize(10);
		PageDataList<BorrowTenderModel> pageDataList = borrowTenderService.getBorrowTenderlist(model);
		data = new HashMap<String, Object>();
		data.put("data", pageDataList);
		data.put("url", SystemConfigHelper.getValue(SystemConfigHelper.Nid.IMAGE_SERVER_URL));
		data.put("resCode", "1");
		printWebJson(getStringOfJpaObj(data));
	}

	/**
	 * 根据标ID获取借款标还款计划
	 * 
	 * @return
	 */
	@Action("/app/user/repayPlan")
	public void repayPlan() throws Exception {
		model.setUser(getSessionUser());
		PageDataList<BorrowCollectionModel> list = borrowCollectionService.getCurrentRepayPlanByModel(model);
		data = new HashMap<String, Object>();
		data.put("data", list);
		data.put("resCode", "1");
		printWebJson(getStringOfJpaObj(data));
	}

	/**
	 * 借款标回款计划
	 * 
	 * @return
	 */
	@Action("/app/user/allRepayPlan")
	public void allRepayPlan() throws Exception {
		model.setUser(getSessionUser());
		PageDataList<BorrowCollectionModel> list = borrowCollectionService.getRepayPlanByModel(model);
		data = new HashMap<String, Object>();
		data.put("data", list);
		data.put("resCode", "1");
		printWebJson(getStringOfJpaObj(data));
	}

	/**
	 * 查找当前用户投资记录
	 * 
	 * @return
	 */
	@Action("/app/user/tenderList")
	public void tenderList() throws Exception {
		model.setUser(getSessionUser());
		model.setSize(5);
		PageDataList<BorrowTenderModel> pageDataList = borrowTenderService.getTenderRecordlist(model);
		data = new HashMap<String, Object>();
		data.put("data", pageDataList);
		data.put("status", model.getStatus());
		data.put("resCode", "1");
		printWebJson(getStringOfJpaObj(data));
	}
}
